package com.bicon.base.data.common.net;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	//@SuppressWarnings({ "deprecation", "resource" })
	public static String post(String url) throws Exception {
		 String result = "";
		 HttpPost  post = new HttpPost (url);
		 HttpClient httpclient = new DefaultHttpClient();
	    // List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//	     nvps.add(new BasicNameValuePair("systemname", "demo"+new Random().nextInt(1000)));
//	     nvps.add(new BasicNameValuePair("systemcode", new Random().nextInt(1000)+""));
//	     nvps.add(new BasicNameValuePair("remark", "demo"+new Random().nextInt(1000)));
	      
	     try {
		    //post.setEntity(new UrlEncodedFormEntity((List<? extends org.apache.http.NameValuePair>) nvps, "UTF-8"));
			HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 30000);
			HttpConnectionParams.setSocketBufferSize(httpclient.getParams(), 1024*2);
			HttpResponse response = httpclient.execute(post);
		    result = EntityUtils.toString(response.getEntity() , "UTF-8").trim();
		    
		} catch (Exception e) {
				e.printStackTrace();
		}finally{
			post.releaseConnection();
			httpclient.getConnectionManager().shutdown();  
		}
		return result;
	}

	//@SuppressWarnings({ "deprecation", "resource" })
	public static String postEntity(String url, Map<String, String> argsMap) throws Exception {
    
		 String result = "";
		 HttpPost  post = new HttpPost (url);
		 HttpClient httpclient = new DefaultHttpClient();
	     List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	     for(Map.Entry<String, String> entry :argsMap.entrySet()){
	    	 nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
	     }
	     try {
		    post.setEntity(new UrlEncodedFormEntity((List<? extends org.apache.http.NameValuePair>) nvps, "UTF-8"));
			HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 30000);
			HttpConnectionParams.setSocketBufferSize(httpclient.getParams(), 1024*2);
			HttpResponse response = httpclient.execute(post);
		    result = EntityUtils.toString(response.getEntity() , "UTF-8").trim();
		    
		} catch (Exception e) {
				e.printStackTrace();
		}finally{
			post.releaseConnection();
			httpclient.getConnectionManager().shutdown();  
		}
		return result;
		

	}

	//@SuppressWarnings("finally")
	public static String get(String url) throws Exception {
//		 String result = "";
//		 HttpGet  GET = new HttpGet (url);
//		 HttpClient httpclient = new DefaultHttpClient();
//	     try {
//		   
//			HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 30000);
//			HttpConnectionParams.setSocketBufferSize(httpclient.getParams(), 1024*2);
//			HttpResponse response = httpclient.execute(GET);
//		    result = EntityUtils.toString(response.getEntity() , "UTF-8").trim();
//		    
//		} catch (Exception e) {
//				e.printStackTrace();
//		}finally{
//			GET.releaseConnection();
//			httpclient.getConnectionManager().shutdown();  
//		}
//		return result;	
		return get(url,30000);
	}
	
	
	public static String get(String url,int timeout) throws Exception {
		 String result = "";
		 HttpGet  GET = new HttpGet (url);
		 HttpClient httpclient = new DefaultHttpClient();
	     try {
		   
			HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), timeout);
			HttpConnectionParams.setSocketBufferSize(httpclient.getParams(), 1024*2);
			HttpResponse response = httpclient.execute(GET);
		    result = EntityUtils.toString(response.getEntity() , "UTF-8").trim();
		    
		} catch (Exception e) {
				e.printStackTrace();
		}finally{
			GET.releaseConnection();
			httpclient.getConnectionManager().shutdown();  
		}
		return result;	
	}

//	private static byte[] getData(HttpEntity httpEntity) throws Exception {
//
//		BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
//
//		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//
//		bufferedHttpEntity.writeTo(byteArrayOutputStream);
//
//		byte[] responseBytes = byteArrayOutputStream.toByteArray();
//
//		return responseBytes;
//
//	}

//	/**
//	 * 将字节数组转换成字符串
//	 * 
//	 * @param bytes
//	 * @return
//	 * @throws UnsupportedEncodingException
//	 */
//
//	private static String bytesToString(byte[] bytes) throws UnsupportedEncodingException {
//		//System.out.println(bytes);
//		if (bytes != null) {
//			String returnStr = new String(bytes, "utf-8");
//			return returnStr;
//		}
//		return null;
//
//	}
	
	public static void main(String[] args) {
		try {
			System.out.println(HttpUtil.get("http://news.sina.com.cn/gov/zt/xjpbdj/"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
